/**
 ******************************* ICE *******************************
 * ========================================================================
 * Project:		iceservices
 * Package:		org.ice4apps.service
 * File:		IceOfficeService.java
 * Author: 		robin
 * Date: 		May 7, 2010
 * Description:	
 * ========================================================================
 * 
 */

package org.ice4apps.service;

import java.util.List;
import java.util.UUID;
import javax.ejb.Stateless;
import org.ice4apps.service.bean.UserAddServiceRequest;
import org.ice4apps.service.bean.UserGetServiceResponse;
import org.ice4apps.service.bean.UserListEntry;
import org.ice4apps.service.bean.UserListGetServiceResponse;
import org.ice4apps.master.dao.Office;
import org.ice4apps.master.dao.User;
import org.ice4apps.master.dataservices.IceMasterDataServiceLocator;
import org.ice4apps.talk.dao.TalkOffice;
import org.ice4apps.talk.dao.TalkUser;
import org.ice4apps.talk.dataservices.IceTalkDataServiceLocator;

@Stateless
public class IceOfficeService implements IceOfficeServiceLocal {

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceOfficeServiceLocal#addUser(java.lang.String, org.ice4apps.service.bean.UserAddServiceRequest)
     */
    public void addUser (String sessionId, UserAddServiceRequest req) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
            req.validate ();
            Office office = IceMasterDataServiceLocator.getIceMasterDataService ().getOfficeByName (req.getOfficeName ());
            User user = new User ();
            String id = UUID.randomUUID ().toString ();
            user.setId (id);
            Long creationDate = System.currentTimeMillis ();
            user.setCreationdate (creationDate);
            user.setUsername (req.getUserName ());
            user.setPassword (req.getPassword ());
            user.setOffice (office);           
            user.setFeatures (Long.MAX_VALUE);
            IceMasterDataServiceLocator.getIceMasterDataService ().addUser (user);
            TalkOffice talkOffice = IceTalkDataServiceLocator.getIceTalkDataService ().getOffice (office.getId ());
            TalkUser talkUser = new TalkUser ();
            talkUser.setId (id);
            talkUser.setUsername (req.getUserName ());
            talkUser.setPassword (req.getPassword ());
            talkUser.setTalkOffice (talkOffice);
            String domain = req.getDomain ();
            if (domain == null || domain.trim ().equals (""))
                domain = user.getOffice ().getOrganization ().getName ();                
            talkUser.setDomain (domain);
            talkUser.setCreationdate (creationDate);
            talkUser.setFeatures (Long.MAX_VALUE);
            talkUser.setAddon (Long.MAX_VALUE);
            IceTalkDataServiceLocator.getIceTalkDataService ().addUser (talkUser);
        } catch (Exception ex1) {
        }
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceOfficeServiceLocal#getUser(java.lang.String, java.lang.String)
     */
    public UserGetServiceResponse getUser (String sessionId, String userName) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
            User user = IceMasterDataServiceLocator.getIceMasterDataService ().getUserByUserName (userName);
            UserGetServiceResponse resp = new UserGetServiceResponse ();
            resp.setOfficeName (user.getOffice ().getName ());
            resp.setUserName (user.getUsername ());
            TalkUser talkUser = IceTalkDataServiceLocator.getIceTalkDataService ().getUser (user.getId ());
            resp.setDomain (talkUser.getDomain ());
            return resp;
        } catch (Exception ex1) {
        }
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceOfficeServiceLocal#getUserList(java.lang.String, java.lang.String)
     */
    public UserListGetServiceResponse getUserList (String sessionId,
                                                   String officeName) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
            List <User> users = IceMasterDataServiceLocator.getIceMasterDataService ().getUserListByOffice (officeName);
            UserListGetServiceResponse resp = new UserListGetServiceResponse ();
            for (User user : users) {
                UserListEntry entry = new UserListEntry ();
                entry.setUserName (user.getUsername ());
                resp.add (entry);
            }
            return resp;
        } catch (Exception ex1) {
        }
        return null;
    }
}
